package com.cos.lession71.slice;

import com.cos.lession71.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //获取按钮组件
        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
        //设置点击监听
        btn1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //创建 intent
                Intent i= new Intent();
                //设置要传递的参数
                i.setParam("username","张三");
                i.setParam("age",18);
                ArrayList<String> hobby  = new ArrayList<>();
                hobby.add("敲代码");
                hobby.add("读书");
                hobby.add("爬山");
                i.setStringArrayListParam("hobby",hobby);
                //设置 要跳转的页面, 要传递的参数
                //present(new NextAbilitySlice(),i);

                //使用需求回传值的跳转  resultCode 回传值的码,用于验证属于回传的数据
                presentForResult(new NextAbilitySlice(),i,123456);
            }
        });
        //在onStart获取组件
        t = (Text) findComponentById(ResourceTable.Id_text_helloworld);

    }
    //方便其他方法使用提取为属性
    Text t;
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        //判断回传值的码是否正确
        if (requestCode==123456) {
            //正确 这个数据是属于回传的数据,进行取值
           String param = resultIntent.getStringParam("param");
            System.out.println(param);
            //把获取的回传值,设值到当前的text文本域中
            t.setText(param);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
